<?php
/**
 * Created by PhpStorm.
 * User: qufan
 * Date: 2016/12/21 0021
 * Time: 23:39
 */

namespace Common\Logic;


class ExcelLogic
{
    public static function read($filename, $encode='utf-8'){
        vendor('PHPExcel.Classes.PHPExcel');
        $info = pathinfo($filename);
        $ext =  $info['extension'];
        if($ext == 'xlsx'){
            $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
        }else{
            $objReader = \PHPExcel_IOFactory::createReader("Excel5");
        }
//        $objReader = \PHPExcel_IOFactory::createReader("Excel5");
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load($filename);
        $objWorksheet = $objPHPExcel->getSheet(0);
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
        $excelData = array();
        for ($row = 1; $row<= $highestRow;$row++){
            for ($col=0;$col<$highestColumnIndex;$col++) {
                $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
            }
        }

        return $excelData;
    }

    public static function importData($class_id, $data,$orgcode=''){
        switch ($class_id){
            case 1:
                self::importHavens($data,$orgcode);
                break;
            case 2:
                self::importReserve($data,$orgcode);
                break;
            case 3:
                self::importProtect($data,$orgcode);
                break;
            case 4:
                self::importRescue($data,$orgcode);
                break;
            case 5:
                self::importDanger($data,$orgcode);
                break;
            case 6:
                self::importMaterial($data,$orgcode);
                break;
            case 7:
                self::importMedia($data,$orgcode);
                break;
            default:
                break;

        }
    }

    protected static function importHavens($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['level'] = $data[$startIndex][6];
            $item['accommodate_number'] = $data[$startIndex][7];
            $item['person_charge'] = $data[$startIndex][8];
            $item['fz_contact_telephone'] = $data[$startIndex][9];
            $item['fz_move_phone'] = $data[$startIndex][10];
            $item['fz_residence_telephone'] = $data[$startIndex][11];
            $item['fz_fax'] = $data[$startIndex][12];
            $item['fz_email'] = $data[$startIndex][13];
            $item['contacts'] = $data[$startIndex][14];
            $item['contacts_office_telephone'] = $data[$startIndex][15];
            $item['contacts_move_telephone'] = $data[$startIndex][16];
            $item['contacts_residence_telephone'] = $data[$startIndex][17];
            $item['contacts_fax'] = $data[$startIndex][18];
            $item['contacts_email'] = $data[$startIndex][19];
            $item['communication_mode'] = $data[$startIndex][20];
            $item['input_time'] = $data[$startIndex][21];
            $item['use_life'] = $data[$startIndex][22];
            $item['anti_seismic'] = $data[$startIndex][23];
            $item['area'] = $data[$startIndex][24];
            $item['basic_situation'] = $data[$startIndex][25];
            $item['traffic_conditions'] = $data[$startIndex][26];
            $item['remarks'] = $data[$startIndex][27];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumHavens')->addAll($dataArr);
    }

    protected static function importReserve($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['level'] = $data[$startIndex][6];
            $item['area'] = $data[$startIndex][7];
            $item['capacity'] = $data[$startIndex][8];
            $item['person_charge'] = $data[$startIndex][9];
            $item['fz_contact_telephone'] = $data[$startIndex][10];
            $item['fz_move_phone'] = $data[$startIndex][11];
            $item['fz_residence_telephone'] = $data[$startIndex][12];
            $item['fz_fax'] = $data[$startIndex][13];
            $item['fz_email'] = $data[$startIndex][14];
            $item['contacts'] = $data[$startIndex][15];
            $item['contacts_office_telephone'] = $data[$startIndex][16];
            $item['contacts_move_telephone'] = $data[$startIndex][17];
            $item['contacts_residence_telephone'] = $data[$startIndex][18];
            $item['contacts_fax'] = $data[$startIndex][19];
            $item['contacts_email'] = $data[$startIndex][20];
            $item['traffic'] = $data[$startIndex][21];
            $item['input_time'] = $data[$startIndex][22];
            $item['affected_form'] = $data[$startIndex][23];
            $item['communication_mode'] = $data[$startIndex][24];
            $item['protected_area'] = $data[$startIndex][25];
            $item['accommodate_number'] = $data[$startIndex][26];
            $item['protected_level'] = $data[$startIndex][27];

            $item['total_number'] = $data[$startIndex][28];
            $item['monitoring_method'] = $data[$startIndex][29];
            $item['reserve_logistics'] = $data[$startIndex][30];
            $item['protection_measures'] = $data[$startIndex][31];
            $item['basic_situation'] = $data[$startIndex][32];
            $item['remarks'] = $data[$startIndex][33];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumReserve')->addAll($dataArr);
    }

    protected static function importProtect($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['protected_level'] = $data[$startIndex][6];
            $item['level'] = $data[$startIndex][7];
            $item['total_number'] = $data[$startIndex][8];
            $item['person_charge'] = $data[$startIndex][9];
            $item['fz_contact_telephone'] = $data[$startIndex][10];
            $item['fz_move_phone'] = $data[$startIndex][11];
            $item['fz_residence_telephone'] = $data[$startIndex][12];
            $item['fz_fax'] = $data[$startIndex][13];
            $item['fz_email'] = $data[$startIndex][14];
            $item['contacts'] = $data[$startIndex][15];
            $item['contacts_office_telephone'] = $data[$startIndex][16];
            $item['contacts_move_telephone'] = $data[$startIndex][17];
            $item['contacts_residence_telephone'] = $data[$startIndex][18];
            $item['contacts_fax'] = $data[$startIndex][19];
            $item['contacts_email'] = $data[$startIndex][20];
            $item['affected_form'] = $data[$startIndex][21];
            $item['communication_mode'] = $data[$startIndex][22];
            $item['influence_range'] = $data[$startIndex][23];
            $item['monitoring_method'] = $data[$startIndex][24];
            $item['threat_number'] = $data[$startIndex][25];
            $item['area_covered'] = $data[$startIndex][26];
            $item['input_time'] = $data[$startIndex][27];
            $item['use_life'] = $data[$startIndex][28];
            $item['protection_measures'] = $data[$startIndex][29];
            $item['traffic'] = $data[$startIndex][30];
            $item['basic_situation'] = $data[$startIndex][31];
            $item['remarks'] = $data[$startIndex][32];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumProtect')->addAll($dataArr);
    }

    protected static function importRescue($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['level'] = $data[$startIndex][6];
            $item['total_number'] = $data[$startIndex][7];
            $item['person_charge'] = $data[$startIndex][8];
            $item['fz_contact_telephone'] = $data[$startIndex][9];
            $item['fz_move_phone'] = $data[$startIndex][10];
            $item['fz_residence_telephone'] = $data[$startIndex][11];
            $item['fz_fax'] = $data[$startIndex][12];
            $item['fz_email'] = $data[$startIndex][13];
            $item['contacts'] = $data[$startIndex][14];
            $item['contacts_office_telephone'] = $data[$startIndex][15];
            $item['contacts_move_telephone'] = $data[$startIndex][16];
            $item['contacts_residence_telephone'] = $data[$startIndex][17];
            $item['contacts_fax'] = $data[$startIndex][18];
            $item['contacts_email'] = $data[$startIndex][19];
            $item['communication_mode'] = $data[$startIndex][20];


            $item['preparation_time'] = $data[$startIndex][21];
            $item['setdeparture_place'] = $data[$startIndex][22];
            $item['setup_time'] = $data[$startIndex][23];

            $item['duty'] = $data[$startIndex][24];
            $item['equipment_description'] = $data[$startIndex][25];
            $item['fund_investment'] = $data[$startIndex][26];

            $item['expertise_description'] = $data[$startIndex][27];
            $item['rescue_experience'] = $data[$startIndex][28];

            $item['remarks'] = $data[$startIndex][29];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumProtect')->addAll($dataArr);
    }

    protected static function importDanger($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['risk_level'] = $data[$startIndex][6];

            $item['person_charge'] = $data[$startIndex][7];
            $item['fz_contact_telephone'] = $data[$startIndex][8];
            $item['fz_move_phone'] = $data[$startIndex][9];
            $item['fz_residence_telephone'] = $data[$startIndex][10];
            $item['fz_fax'] = $data[$startIndex][11];
            $item['fz_email'] = $data[$startIndex][12];
            $item['contacts'] = $data[$startIndex][13];
            $item['contacts_office_telephone'] = $data[$startIndex][14];
            $item['contacts_move_telephone'] = $data[$startIndex][15];
            $item['contacts_residence_telephone'] = $data[$startIndex][16];
            $item['contacts_fax'] = $data[$startIndex][17];
            $item['contacts_email'] = $data[$startIndex][18];

            $item['communication_mode'] = $data[$startIndex][19];
            $item['disaster_form'] = $data[$startIndex][20];
            $item['influence_range'] = $data[$startIndex][21];
            $item['monitoring_method'] = $data[$startIndex][22];
            $item['threat_number'] = $data[$startIndex][23];
            $item['area_covered'] = $data[$startIndex][24];
            $item['input_time'] = $data[$startIndex][25];

            $item['use_life'] = $data[$startIndex][26];
            $item['control_measures'] = $data[$startIndex][27];
            $item['traffic'] = $data[$startIndex][28];
            $item['basic_situation'] = $data[$startIndex][29];
            $item['remarks'] = $data[$startIndex][30];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumHazardSource')->addAll($dataArr);
    }

    protected static function importMaterial($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['level'] = $data[$startIndex][6];
            $item['investment_date'] = $data[$startIndex][7];

            $item['person_charge'] = $data[$startIndex][8];
            $item['fz_contact_telephone'] = $data[$startIndex][9];
            $item['fz_move_phone'] = $data[$startIndex][10];
            $item['fz_residence_telephone'] = $data[$startIndex][11];
            $item['fz_fax'] = $data[$startIndex][12];
            $item['fz_email'] = $data[$startIndex][13];
            $item['contacts'] = $data[$startIndex][14];
            $item['contacts_office_telephone'] = $data[$startIndex][15];
            $item['contacts_move_telephone'] = $data[$startIndex][16];
            $item['contacts_residence_telephone'] = $data[$startIndex][17];
            $item['contacts_fax'] = $data[$startIndex][18];
            $item['contacts_email'] = $data[$startIndex][19];
            $item['production_materials'] = $data[$startIndex][20];
            $item['production_ability'] = $data[$startIndex][21];

            $item['remarks'] = $data[$startIndex][22];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumMaterial')->addAll($dataArr);
    }

    protected static function importMedia($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dataArr = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $item = array();
            $item['name'] = $data[$startIndex][0];
            $item['address'] = $data[$startIndex][1];
            $type = $data[$startIndex][2];
            $typ_arr = explode('_', $type);
            if(count($typ_arr) == 2){
                $item['type'] = $typ_arr[1];
            }
            $item['longitude'] = $data[$startIndex][3];
            $item['latitude'] = $data[$startIndex][4];
            $item['administrative_division'] = $data[$startIndex][5];
            $item['level'] = $data[$startIndex][6];
            $item['mechanism_level'] = $data[$startIndex][7];
            $item['unique_code'] = $data[$startIndex][8];
            $item['person_charge'] = $data[$startIndex][9];
            $item['fz_contact_telephone'] = $data[$startIndex][10];
            $item['fz_move_phone'] = $data[$startIndex][11];
            $item['fz_residence_telephone'] = $data[$startIndex][12];
            $item['fz_fax'] = $data[$startIndex][13];
            $item['fz_email'] = $data[$startIndex][14];
            $item['contacts'] = $data[$startIndex][15];
            $item['contacts_office_telephone'] = $data[$startIndex][16];
            $item['contacts_move_telephone'] = $data[$startIndex][17];
            $item['contacts_residence_telephone'] = $data[$startIndex][18];
            $item['contacts_fax'] = $data[$startIndex][19];
            $item['contacts_email'] = $data[$startIndex][20];
            $item['communication_mode'] = $data[$startIndex][21];
            $item['disaster_form'] = $data[$startIndex][22];
            $item['number_of_beds'] = $data[$startIndex][23];
            $item['number_of_nurses'] = $data[$startIndex][24];
            $item['ambulance_number'] = $data[$startIndex][25];
            $item['treatment_subject'] = $data[$startIndex][26];
            $item['treatment_equipment'] = $data[$startIndex][27];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;
            $dataArr[] = $item;
        }

        D('DatumHygiene')->addAll($dataArr);
    }

    public static function importContact($data,$orgcode=''){

        $count = count($data);
        if ($count <=1){
            return;
        }
        $groups = M('MailListGroup')->select();
        $groupDic = array();
        foreach ($groups as $group){
            $groupDic[$group['group_name']] = $group['group_code'];
        }
        $dataArr = array();
        $success = 0;
        $failed = 0;
        $errLines = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            $group_name = $data[$startIndex][0];
            if (!isset($groupDic[$group_name])){
                $failed ++;
                $errLines[] = $startIndex - 1;
                continue;
            }
            $success ++;
            $item = array();
            $item['group_code'] = $groupDic[$group_name];
            $item['real_name'] = $data[$startIndex][1];
            $item['sex'] = trim($data[$startIndex][2]) == '男'? 0 : 1;
            if($item['sex'] == 0){
                $item['icon'] = C('DEFAULT_USER_ICON.MALE');
            }else{
                $item['icon'] = C('DEFAULT_USER_ICON.FEMALE');
            }
            $item['company'] = $data[$startIndex][3];
            $item['position'] = $data[$startIndex][4];
            $item['phone'] = $data[$startIndex][5];
            $item['office_tel'] = $data[$startIndex][6];
            $item['residence_tel'] = $data[$startIndex][7];
            $item['describe'] = $data[$startIndex][8];
            $item['add_time'] = time_format(NOW_TIME);
            $item['state'] = 1;
            $dataArr[] = $item;
        }

        $res = D('MailList')->addAll($dataArr);
        $ret['success'] = $success;
        $ret['failed'] = $failed;
        $ret['errLines'] = $errLines;
        return $ret;

    }


    public static function importDuty($data,$orgcode=''){
        $count = count($data);
        if ($count <=1){
            return;
        }
        $dutyClassModel = D('DutyClass');
        $dutyArrangeModel = D('DutyArrange');
        $success = 0;
        $failed = 0;
        $errLines = array();
        for ($startIndex = 2; $startIndex <= $count; $startIndex ++){
            if (empty(trim($data[$startIndex][0]))){
                break;
            }
            $success ++;
            $item = array();
            $duty = array();


            $holiday = trim($data[$startIndex][4]);
            $holiday_type_id = 0;
            switch ($holiday){
                case '工作日':
                    $holiday_type_id = 0;
                    break;
                case '公休日':
                    $holiday_type_id = 1;
                    break;
                case '节假日':
                    $holiday_type_id = 2;
                    break;
                default:
                    break;
            }
            $duty['holidaytypeid'] = $holiday_type_id;
            $duty['duty_type_code'] =trim($data[$startIndex][2]);
            $duty['duty_type_name'] = trim($data[$startIndex][3]);
            $duty['sort_num'] = 0;
            $duty['state'] = 1;

            $duty_type_id = $dutyClassModel->update($duty);

            if($duty_type_id == false){
                $failed ++;
                $errLines[] = $startIndex - 1;
                continue;
            }

            $item['duty_type_id'] = $duty_type_id;
            $item['duty_user'] = trim($data[$startIndex][1]);
            $item['duty_date'] = trim($data[$startIndex][0]);
            $item['state'] = 1;
            $item['orgcode'] = $orgcode;

            $duty_arrange_id = $dutyArrangeModel->update($item);
            if($duty_arrange_id == false){
                $failed ++;
                $errLines[] = $startIndex - 1;
                continue;
            }
        }


        $ret['success'] = $success;
        $ret['failed'] = $failed;
        $ret['errLines'] = $errLines;
        return $ret;
    }


}